package com.category.douyijia.utils;

import java.security.MessageDigest;



import sun.misc.*;
/**
 * setting the methods to encryption the message.
 * it can encrypted user password and it have a lot of
 * encryption type.
 * it can encrypted user message ,give a token to shiro
 * and help user login to the web.
 * @author JunfengLiu
 * @version v1.0
 * date time: 2021-7-17 19:14:00
 * 
 */
public class Encryption {
	/**
	 * encrypted the password.
	 * it is type's of md5.
	 * @param userPassword
	 * user login password.
	 * @return
	 * the encrypted password char.
	 * @throws Exception
	 * throw convert exception.
	 */
	/*
	 * 加密密码，
	 * 加密类型为MD5加密。
	 * @param userPassword
	 * 用户密码
	 * @return
	 * 加密后的用户密码
	 * @throws Exception
	 * 抛出加密异常
	 */
	public static String passwordMD5(String userPassword) throws Exception{
		//create a variable saved to md5 encrypted password value.
		//
		String MD5password = null;
		//judge the value weather a null value.
		if (userPassword != null &&!(userPassword.equals(""))) {
			//if value not null,create a md5 methods object.
			MessageDigest md5 = MessageDigest.getInstance("MD5");
			//create base 64 encoder encryption object
			BASE64Encoder base64en = new BASE64Encoder();
			//encrypted the password.
			MD5password = base64en.encode(
											md5.digest(
											userPassword.getBytes("utf-8")
											)
										);
			return MD5password;
		}else {
			return MD5password;
		}
	}
//	/**
//	 * test encryption methods.
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		Encryption ep = new Encryption();
//		try {
//			String password = "wulixuezhe0317wulixuezhe0317";
//			String test = ep.passwordMD5(password);
//			System.out.println("加密前："+password+"加密后为："+test);
//			System.out.println("加密后长度为："+test.length());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}finally {
//			System.out.println("结束！");
//		}
//	}
}
